#pragma once 
#include <iostream>
#include <string>
#include <fstream>
#include <vector>
#include <boost/algorithm/string.hpp>
#include "cppjieba_/Jieba.hpp"
#include "log.hpp"
#include <mutex>
namespace ns_util
{
    class FileUtil
    {
    public:
        static bool ReadFile(const std::string& file_path, std::string* out)
        {
            std::ifstream in(file_path, std::ios::in);
            if(!in.is_open())
            {
                std::cerr << "open file " << file_path << " error " << std::endl;
                return false;
            }
            std::string line;
            while(std::getline(in, line)) //geline返回了是一个流， 这里重载了强制类型转换
            {
                *out += line;
            }
            in.close();
            return true;
        }
    };

    class StringUtil
    {
    public:
        static void Split(const std::string& target, std::vector<std::string>* out, const std::string& sep)
        {
            //boost的切分字符串，第一个参数目标vector,第二个参数目标字符串，第三个分隔符，第四个代表是否压缩，这里选择压缩
            boost::split(*out, target, boost::is_any_of(sep), boost::token_compress_on);
        }
    };
    const char* const DICT_PATH = "./dict/jieba.dict.utf8";
    const char* const HMM_PATH = "./dict/hmm_model.utf8";
    const char* const USER_DICT_PATH = "./dict/user.dict.utf8";
    const char* const IDF_PATH = "./dict/idf.utf8";
    const char* const STOP_WORD_PATH = "./dict/stop_words.utf8";
    class JiebaUtil
    {
    public:
        static JiebaUtil* get_insatnce()
        {
            static std::mutex mtx;
            if(nullptr == instance)
            {
                mtx.lock();
                if(nullptr == instance)
                {
                    instance = new JiebaUtil();
                    instance->InitJiebaUtil();
                }
                mtx.unlock();
            }
            return instance;
        }
        void InitJiebaUtil()
        {
            std::ifstream in(STOP_WORD_PATH);
            if(!in.is_open())
            {
                LOG(FATAL, "load stop words file error");
                return;
            }
            std::string line;
            while(std::getline(in, line))
            {
                stop_words.insert({line, true});
            }

            in.close();
        }
        void CutStringHelper(const std::string& src, std::vector<std::string>* out)
        {
            jieba.CutForSearch(src, *out);
            for(auto iter = out->begin(); iter != out->end(); )
            {
                auto it = stop_words.find(*iter);
                if(it != stop_words.end())
                {
                    //当前的string是暂停词，需要去掉
                    iter = out->erase(iter);
                }
                else
                {
                    iter++; //防止迭代器失效
                }
            }
        }
        static void CutString(const std::string& src, std::vector<std::string>* out)
        {
            //jieba.CutForSearch(src, *out);
            ns_util::JiebaUtil::get_insatnce()->CutStringHelper(src, out);
        }
    private:
      //  static cppjieba::Jieba jieba;
        cppjieba::Jieba jieba;
        std::unordered_map<std::string, bool> stop_words;
    private:
        //单例
        JiebaUtil():jieba(DICT_PATH, HMM_PATH, USER_DICT_PATH, IDF_PATH, STOP_WORD_PATH){}
        JiebaUtil(const JiebaUtil&) = delete;
        static JiebaUtil* instance;
    };
    JiebaUtil* JiebaUtil::instance = nullptr;
    //cppjieba::Jieba JiebaUtil::jieba(DICT_PATH, HMM_PATH, USER_DICT_PATH, IDF_PATH, STOP_WORD_PATH);
}